import java.applet.AppletContext;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;


public class Display extends JApplet implements MouseMotionListener,MouseListener{

	
	static MediaTracker mt;
    static URL base;
	
	int levelWidth = 25, levelHeight = 25;
	int tileSize = 20;
	int board[][] = new int[50][50];
	boolean enemy[][] = new boolean[50][50];
	boolean player[][] = new boolean[50][50];

	boolean hit[][] = new boolean[50][50];

	int enemyPieces = 9;
	int playerPieces = 9;
	Image sea;
	Image boat;
	Image PlayerBoat;
	Image menu;
	Image quit;
	
	boolean isMenu = true;
	boolean isReady = false;
	
	public void init(){
		

		addMouseMotionListener((MouseMotionListener)this);
		addMouseListener((MouseListener)this);
		
		mt = new MediaTracker(this);
    	base = getDocumentBase(); 

   	 
    	sea = getImage(base,"Images/sea.JPG");
    	boat = getImage(base,"Images/boat.JPG");
    	PlayerBoat = getImage(base,"Images/PlayerBoat.JPG");
    	menu = getImage(base,"Images/Start_button.JPG");
    	quit = getImage(base,"Images/Quit_button.JPG");

    	
    	
    	mt.addImage(sea ,1);
    	mt.addImage(boat ,2);
    	mt.addImage(PlayerBoat ,3);
    	mt.addImage(menu ,4);
    	mt.addImage(quit ,5);
    	
    	try {
			mt.waitForAll();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

		
	}
	public void placeBoat(){
		if(playerPieces < 0){
			setup();
			test();
			isReady = true;
		}else if(player[mouseX][mouseY] == false){
			player[mouseX][mouseY] = true;
			playerPieces--;
		}

	}
	public void test(){
		int x = 0 + (int)(Math.random()*levelWidth);
		int y = 0 + (int)(Math.random()*levelHeight);
		if(!(enemy[x][y] == true) && !(player[mouseX][mouseY] == true)){
			enemy[x][y] = true;
			enemyPieces--;
		}
	}
	public void setup(){
		
		
		while(!(enemyPieces == 0)){
			test();
		}
		
	}
	public void menu(){
		if(mouseX > 6 && mouseX < 16 && mouseY > 5 && mouseY < 9){
			isMenu = false;
			
		}else{
			System.out.println("sklkdjflksdjflsjdfsflksjdlfjsd");
			String link = "http://captharryhorne.webs.com/";
			  try
			  {
			  AppletContext a = getAppletContext();
			  URL url = new URL(link);
			  a.showDocument(url,"_self");
			//  a.showDocument(url,"_self");
			//  _self to open page in same window  
			  }
			  catch (MalformedURLException e){
			  System.out.println(e.getMessage());
			  }
		}
		
	}
	
	public void clicked(){
		if(enemy[mouseX][mouseY] == true){
			enemy[mouseX][mouseY] = false;
			hit[mouseX][mouseY] = true;
			System.out.println("Removed Ship");
		}
	}
	Image dbImage;
    Graphics dbg;
    public void paint(Graphics g){
   	 dbImage = createImage(getWidth(), getHeight());
   	 dbg = dbImage.getGraphics();
   	 paintScreen(dbg);
   	 g.drawImage(dbImage, 0, 0, this);
    }
    public void paintScreen(Graphics g){
    	
    	if(isMenu == true){
    		g.setColor(Color.black);
    		g.fillRect(0, 0, 500, 500);
    		g.drawImage(menu, 100, 100, 300, 100, null);
    		g.drawImage(quit, 100, 300, 300, 100, null);
    	}else{
    	
	    	for(int x = 0; x < levelWidth; x++){
	    		for(int y = 0; y < levelHeight; y++){
					g.setColor(Color.black);
	
	    			g.drawImage(sea,x*tileSize,y*tileSize,tileSize,tileSize,null);
	    			
	    			if(player[x][y] == true){
	        			g.drawImage(PlayerBoat,x*tileSize,y*tileSize,tileSize,tileSize,null);
	    			}
	    			
	    			if(enemy[x][y] == true){
	        			g.drawImage(boat,x*tileSize,y*tileSize,tileSize,tileSize,null);
	    			}
	    			if(hit[x][y] == true){
	    				g.setColor(Color.red);
	    				g.fillRect(x*tileSize,y*tileSize,tileSize,tileSize);
	    			}
	    		}
	    	}
    	}
    }
    
    
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		System.out.println("Pressed " + mouseX + "   " + mouseY);
		if(isMenu == true){
			menu();
		}
		else if(isReady == false){
			placeBoat();
		}else{
			clicked();
		}
		repaint();
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseDragged(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	int mouseX = 0, mouseY = 0;
	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		mouseX = e.getX()/tileSize;
		mouseY = e.getY()/tileSize;
	}

}
